package com.atguigu.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.vo.ExcelSubjectData;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//监听器使用是直接new，不是通过框架注入的。不能交给框架管理
//service不能自动注入，采用手动注入，使用有参构造。
public class SubjectExcelListener extends AnalysisEventListener<ExcelSubjectData> {

    private EduSubjectService eduSubjectService;

    public SubjectExcelListener() {
    }

    public SubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }

    @Override
    public void invoke(ExcelSubjectData excelSubjectData, AnalysisContext analysisContext) {
        //1、验空
        if(excelSubjectData==null){
                throw new GuliException(20001,"课程分类信息失败");
        }

        //2、读取一级分类数据
        String oneSubjectName = excelSubjectData.getOneSubjectName();
        //3、判断一级分类是否重复（一级名称）
        EduSubject existOneSubject = this.existOneSubject(eduSubjectService, oneSubjectName);
        if(existOneSubject==null){
            //4、如果数据库没有，插入数据库
            existOneSubject = new EduSubject();
            existOneSubject.setParentId("0");
            existOneSubject.setTitle(oneSubjectName);
            eduSubjectService.save(existOneSubject);
        }
        String pid = existOneSubject.getId();
        //5、读取二级分类数据
        String twoSubjectName = excelSubjectData.getTwoSubjectName();
        //6、判断二级分类是否重复（二级名称、一级id）
        EduSubject existTwoSubject = this.existTwoSubject(eduSubjectService,twoSubjectName,pid);
        if(existTwoSubject==null){
            //7、如果数据库没有，插入数据库
            existTwoSubject = new EduSubject();
            existTwoSubject.setParentId(pid);
            existTwoSubject.setTitle(twoSubjectName);
            eduSubjectService.save(existTwoSubject);
        }

    }
    //判断一级分类是否重复（一级名称）
    private EduSubject existOneSubject(EduSubjectService subjectService,
                                       String oneSubjectName){
        QueryWrapper<EduSubject>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id","0");
        queryWrapper.eq("title",oneSubjectName);
        EduSubject oneSubject = subjectService.getOne(queryWrapper);
        return oneSubject;
    }
    //判断一级分类是否重复（二级名称、一级id）
    private EduSubject existTwoSubject(EduSubjectService subjectService,
                                       String twoSubjectName,
                                       String pid){
        QueryWrapper<EduSubject>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",pid);
        queryWrapper.eq("title",twoSubjectName);
        EduSubject twoSubject = subjectService.getOne(queryWrapper);
        return twoSubject;

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
